package cn.lin.controller;

import cn.lin.domain.User;
import cn.lin.service.EditPasswordService;
import cn.lin.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pwd")
public class PasswordController {
    @Autowired
    private EditPasswordService editPasswordService;

    @RequestMapping("validPassword")
    public Object validPassword(@RequestParam("id") Integer id,
                                @RequestParam("pwd") String pwd,
                                @RequestParam("newpassword") String newpassword){
        User user = editPasswordService.findById(id);
        if(user != null){
            if (user.getPwd().equals(pwd)){
                int i = editPasswordService.updatePassword(newpassword, id);
                if (i == 1){
                    Result<String> result = new Result<>(1001, "修改密码成功！");
                    return result;
                }else {
                    return new Result<String>(1002, "修改密码失败");
                }
            }
        }
        Result<String> result = new Result<>(1002, "密码不一致！");
        return result;
    }

    @GetMapping("/update")
    public Object updatePassword(@RequestParam("newpassword") String newpassword,
                         @RequestParam("id") Integer id){
        int i = editPasswordService.updatePassword(newpassword, id);
        if (i == 1) {
            Result<String> result = new Result<>(1001, "更新成功！");
            return result;
        }else {
            Result<String> result = new Result<>(1002, "更新失败！");
            return result;
        }
    }
}
